﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.BLL;
using SIS.ENT;

public partial class Employee_Photo_Upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {           

            if (Request.Params["id"] != null)
            {

                int _accountId = 0;
                if (Int32.TryParse(Request.Params["id"].ToString(), out _accountId))
                {
                    LoadEmployeeInfo(_accountId);
                }
                else
                {
                    lblUserAccountKey.Text = "-1";
                }

            }

        }
    }

    private void LoadEmployeeInfo(int AccountID)
    {
        UserAccountTable _account = new UserAccountTable();
        _account = UserAccountTableManager.GetItem(AccountID);         

        if (_account != null && _account.UserAccountKey > 0)
        {
            

            lblUserAccountKey.Text = _account.UserAccountKey.ToString();

            EmployeeProfileTable _sprofile = new EmployeeProfileTable();
            EmployeeDesignationTable _tdesignation = new EmployeeDesignationTable();
             

            _sprofile = EmployeeProfileTableManager.GetItemByUserAccountKey(AccountID, _account.SchoolKey);
            if (_sprofile != null && _sprofile.EmployeeProfileKey > 0)
            {
                lblEmployeeProfileKey.Text = _sprofile.EmployeeProfileKey.ToString();

                string _fullName = string.Empty;

                _fullName = _sprofile.FirstName;

                if (_fullName.Length == 0)
                {
                    _fullName = _sprofile.LastName;
                }
                else
                {
                    _fullName += " " + _sprofile.LastName;
                }

                if (_sprofile.OtherName != null && _sprofile.OtherName.Length > 0)
                {
                    _fullName = _sprofile.OtherName;
                }

                lblFullName.Text = _fullName;

                if (_sprofile.Photo != null && _sprofile.Photo.Length > 0)
                {
                    imgCurrentEmployeePhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + lblEmployeeProfileKey.Text + "&type=Employeephoto";
                    hplCurrentEmployeePhoto.NavigateUrl = imgCurrentEmployeePhoto.ImageUrl;
                    imgCurrentEmployeePhoto.Width = 100;
                    imgCurrentEmployeePhoto.Height = 150;
                }
                else
                {
                    imgCurrentEmployeePhoto.ImageUrl = "";
                    hplCurrentEmployeePhoto.NavigateUrl = "";
                    imgCurrentEmployeePhoto.Width = 0;
                    imgCurrentEmployeePhoto.Height = 0;
                }

                _tdesignation = EmployeeDesignationTableManager.GetActiveDesignationItem(_sprofile.UserAccountKey);
                if (_tdesignation != null)
                {
                    JobDesignationCodeTable _jdcode = new JobDesignationCodeTable();
                    _jdcode = JobDesignationCodeTableManager.GetItem(_tdesignation.JobDesignationCode);
                    if (_jdcode != null)
                    {
                        lblCurrentDesignation.Text = _jdcode.Description;
                        lblCurrentDesignation.Text += "<br/><br/>Job Desc:<br/>" + _tdesignation.JobDescriptionDetail;
                    }
                }
            }


             

        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Upload failed!";

        int _fileLen = 0;
        string _fileName = string.Empty;
        string _fileExt = string.Empty;

        bool _fileExtValid = false;

        string[] _arrFileExt = System.Configuration.ConfigurationManager.AppSettings["SchoolImageUploadFileExt"].Split(',');

        int _PhotoFileSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["StudentUploadLogoFileSizeKB"]);


        if (fupPhoto.HasFile && fupPhoto.PostedFile != null)
        {
            if (fupPhoto.PostedFile.ContentLength > 0)
            {
                _fileLen = fupPhoto.PostedFile.ContentLength / 1024;
            }

            _fileName = fupPhoto.PostedFile.FileName;
            _fileExt = System.IO.Path.GetExtension(_fileName).ToLower();

            for (int i = 0; i < _arrFileExt.Length; i++)
            {
                if (_fileExt == _arrFileExt[i])
                {
                    _fileExtValid = true;
                    break;
                }
            }

            if (_fileExtValid)
            {
                if (_fileLen > 0 && _fileLen <= _PhotoFileSize)
                {
                    Byte[] imgPhoto = null;
                    //To create a PostedFile
                    HttpPostedFile File = fupPhoto.PostedFile;
                    //Create byte Array with file len
                    imgPhoto = new Byte[File.ContentLength];
                    //force the control to load data in array
                    File.InputStream.Read(imgPhoto, 0, File.ContentLength);

                    if (EmployeeProfileTableManager.UpdatePhoto(Convert.ToInt32(lblEmployeeProfileKey.Text), imgPhoto))
                    {
                        lblMessage.Text = "<br/>Update photo succeeded!";
                        imgCurrentEmployeePhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + lblEmployeeProfileKey.Text + "&type=Employeephoto";
                        hplCurrentEmployeePhoto.NavigateUrl = imgCurrentEmployeePhoto.ImageUrl;
                        imgCurrentEmployeePhoto.Width = 100;
                        imgCurrentEmployeePhoto.Height = 150;
                    }
                    else
                    {
                        lblMessage.Text = "<br/>Update photo failed!";
                    }
                    imgPhoto = null;
                    File = null;
                }
                else
                {
                    lblMessage.Text = "<br/>Photo size is over limit!";
                }
            }
            else
            {
                lblMessage.Text += "<br/>Photo file type is NOT allowed to update!";
            }
        }
        else
        {
            lblMessage.Text = "No photo file was uploaded!";
        }
    }

    protected void btnViewSchoolProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("profile.aspx");
    }

    protected void btnViewEmployeeProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("Employeeentry.aspx?id=" + lblUserAccountKey.Text);
    }
}